import {
    Controller, Get, Param, Query,
    ParseIntPipe, ParseFloatPipe, ParseBoolPipe, ParseArrayPipe, ParseUUIDPipe, ParseEnumPipe,DefaultValuePipe
} from "@nestjs/common";
import {CustomValidatePipe} from './customPipes/validate.pipe'

enum Roles {
    Admin = 'admin',
    VIP = 'VIP'
}

@Controller('app')
export class AppController {
    @Get('number/:id')
    getNumber(@Param('id', ParseIntPipe) id: number) {
        let num = 100;      //测试是否转为了数值型
        return `Number:${num + id}`
    }

    @Get('float/:float')
    getFloat(@Param('float', ParseFloatPipe) float: number) {
        let num = 100;      //测试是否转为了数值型
        return `Number:${num + float}`
    }

    @Get('boolean/:flat')
    getBoolean(@Param('flat', ParseBoolPipe) flat: boolean) {
        if (flat) {
            return 'app/boolean/true：这个结果为true字符串的'
        } else {
            return 'app/boolean/true：这个结果为false字符串的'
        }
    }

    @Get('array/:list')     //参数逗号分割的
    getArray(@Param('list', ParseArrayPipe) list: any[]) {
        // ParseArrayPipe直接使用报错
        // ERROR [PackageLoader] The "class-validator" package is missing. Please, make sure to install it to take advantage of ValidationPipe.
        // [nodemon] app crashed - waiting for file changes before starting...

        //  ERROR [PackageLoader] The "class-transformer" package is missing. Please, make sure to install it to take advantage of ValidationPipe.
        // [nodemon] app crashed - waiting for file changes before starting...
        // 安装一下以来 pnpm install class-validator class-transformer
        console.log(list)
        return list;
    }

    @Get('array-split-symbol/:list')     //参数逗号分割的
    getArraySplitSymbol(@Param('list', new ParseArrayPipe({items: Boolean, separator: '@'})) list: any[]) {
        // 自定义数组参数传递分隔符 @   /app/array-split-symbol/1@2@3@2@1
        console.log(list);
        return list;
    }

    @Get('array-split-symbol2/:list')     //参数逗号分割的
    getArraySplitSymbol2(@Param('list', ParseArrayPipe) list: any[]) {
        console.log(list);
        return list;
    }

    @Get('uuid/:uuid')
    getUuid(@Param('uuid', ParseUUIDPipe) uuid: string) {
        // 9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d  例子
        console.log(uuid);
        return uuid;
    }

    @Get('enum/:value')
    getEnum(@Param('value', new ParseEnumPipe(Roles)) value: string) {
        console.log(value);
        return value;
    }

    // defaultValue @Query
    @Get('defaultValue')
    getDefaultValue(@Query('username', new DefaultValuePipe('默认名字')) username: string) {
        console.log(username);
        return username;
    }

    // customPipe
    @Get('customPipe/:value')
    getCustomPipe(@Param('value', CustomValidatePipe) value: string) {
        console.log(value)
        return value;
    }
}
